﻿CREATE VIEW dbo.v_CF_Test
AS
SELECT     ISNULL(CF_E.ApplID, 0) AS ApplID, S.Name AS Status, CF_E.DPD AS DaysPastDue, CF_E.LoanID, CF_E.[Customer #], CF_E.[Open Field#01], CF_E.SSN,
                       CF_E.VIN, CF_E.Serial#, dbo.t_Loan.StatusID, CF_E.[G_L Date], DATEDIFF(dd, CF_E.[G_L Date], GETDATE()) AS GL_Days, CF_E.[Open Date], 
                      CF_E.Name, CF_E.Address, CF_E.City, CF_E.St, CF_E.[Zip Code], CF_E.[Home Phone #], CF_E.[Work Phone #], CF_E.[Car Year], CF_E.Make, 
                      CF_E.Model, CF_E.[Pymnt Schedule], CF_E.[# of Payments], CF_E.[# of PaymentsM], CF_E.[Payment Amount], CF_E.[Total Payments], 
                      CF_E.[1st_ Payment Date], CF_E.FirstPaymentDateCalc, CF_E.[Last Payment Date], CF_E.[Next Payment Date], CF_E.[# Pmts Remaining], 
                      CF_E.[# Pmts RemainingM], CF_E.[Balance Remaining], CF_E.[Total Sales Price], CF_E.[Finance Charges], CF_E.[Status Code], CF_E.P_O_E_, 
                      CF_E.Loan#_Comments, CF_E.[Primary SSN], CF_E.[Amt_ Fin__Disc_ Amt], CF_E.[Interest Income], CF_E.[Earned Discount], CF_E.[Dealer Discount], 
                      CF_E.[Dealer Proceed], CF_E.[Dealer Code], usix.t_Dealer.Name AS Dealer, usix.t_Dealer.[Phone #] AS DealerPhone, CF_E.[Principal Balance], 
                      CF_E.APR, CF_E.Mileage, CF_E.[Collector Id#], CF_E.[Discount%], CF_E.[Origination State Code], CF_E.Portfolio#, CF_E.[Sales Rep_], 
                      CF_E.[Investor Client], CF_E.[Date L_C], CF_E.[Joint Name], CF_E.[Joint Address], CF_E.[Joint City], CF_E.[Joint State], CF_E.[Joint Zipcode], 
                      CF_E.[Joint SSN], CF_E.[Joint H Phone#], CF_E.[Joint W Phone#], CF_E.[Primary Cell#], CF_E.[Joint Cell#], CF_E.[Partial Payment], CF_E.[NSF Balance], 
                      CF_E.[Late Chg Bal], CF_E.[Apply Late Fee], CF_E.Branch#, CF_E.CompanyID, CF_E.CompanyID_Raw, CF_E.CompanyName, 
                      CF_E.[Bankruptcy Filed Y_N?], CF_E.[Bankruptcy Type 07_13?], CF_E.[Total Principal], CF_E.DPD, CF_E.DSLP, 
                      CF_E.LatePmtsCount * CF_E.[Payment Amount] - ISNULL(CF_E.[Partial Payment], 0) + ISNULL(CF_E.LateCharges, 0) + ISNULL(CF_E.[NSF Balance], 0) 
                      + ISNULL(CF_E.[Late Chg Bal], 0) AS PastDueAmount, CF_E.PaymentsMade, CF_E.PaymentsMadeM, CF_E.PaymentsExpectedM, 
                      CASE WHEN [status code] IN ('O', 'L') THEN CF_E.PaymentsExpectedM - CF_E.PaymentsMadeM ELSE 0 END AS PaymentsBehindM, 
                      CF_E.DownPayment, CF_E.[Princ/ChOff Ballance], CF_E.[Repo Date], CF_E.[A_R Balance], CF_E.[Resale Amt_], CF_E.Chargeoff, 
                      CF_E.[Final Repo Date], CF_E.Retainage, CF_E.[Other Chgs Bal], CF_E.UsixID, ISNULL(CF_E.[Title Received Y_N?], '') AS [Title Received Y_N?], 
                      CF_E.NDay, N_L.DateDue AS [Due Date], dbo.Today(N_L.DateDueBy) AS DateDueBy, N_L.NoteUser AS NoteBy, dbo.ts_TimeZone.TimeZone, 
                      dbo.ts_TimeZone.Offset AS TimeZoneOffset, dbo.t_ContractHolder.Nick AS ContractHolder, ISNULL(N_PTP_O.DaysToPTP, 999) AS DaysToPTP, 
                      N_PTP_O.DateDueBy AS PTP_Agent, CF_E.DPDCalc, N_L.StatusDate AS NoteStatusDate, N_L.NoteDate, CF_E.[Simple_Add-On], CF_E.VINCheck, 
                      CF_E.LateCharges, CF_E.LatePmtsCount, CF_E.RemBalance78, ISNULL(CF_E.[Total Principal], 0) + ISNULL(CF_E.DSLP * CF_E.PerDiem, 0) 
                      + ISNULL(CF_E.LateCharges, 0) + ISNULL(CF_E.[NSF Balance], 0) AS PayOff_TP, 
                      CASE [Simple_Add-On] WHEN 'S1' THEN [Total Principal] + DSLP * PerDiem ELSE RemBalance78 END + CF_E.LateCharges + CF_E.[NSF Balance] AS PayOff_Calc,
                       CF_E.PerDiem, ISNULL(dbo.t_BuyBack.StatusID, dbo.fs_StateID__BuyBack__InActive()) AS StatusID_BuyBack, S_BB.Name AS Status_BuyBack, 
                      ISNULL(dbo.t_Car.TrackerTypeID, 0) AS TrackerTypeID, N_L.NoteDate0, N_L.NoteDate1
FROM         dbo.ts_State INNER JOIN
                      dbo.ts_TimeZone ON dbo.ts_State.TimeZone = dbo.ts_TimeZone.TimeZone AND dbo.ts_State.TimeZone = dbo.ts_TimeZone.TimeZone AND 
                      dbo.ts_State.TimeZone = dbo.ts_TimeZone.TimeZone RIGHT OUTER JOIN
                      usix.t_CustomerFile_Export AS CF_E ON dbo.ts_State.StateCode = CF_E.St LEFT OUTER JOIN
                      dbo.t_Car ON CF_E.VIN = dbo.t_Car.VIN LEFT OUTER JOIN
                      dbo.v_Note_PTP_Open AS N_PTP_O ON CF_E.LoanID = N_PTP_O.LoanID LEFT OUTER JOIN
                      dbo.t_BuyBack INNER JOIN
                      dbo.t_State AS S_BB ON dbo.t_BuyBack.StatusID = S_BB.ID ON CF_E.LoanID = dbo.t_BuyBack.LoanID LEFT OUTER JOIN
                      dbo.t_ContractHolder INNER JOIN
                      dbo.t_Loan ON dbo.t_ContractHolder.ID = dbo.t_Loan.ContractHolderID AND dbo.t_ContractHolder.ID = dbo.t_Loan.ContractHolderID INNER JOIN
                      dbo.t_State AS S ON dbo.t_Loan.StatusID = S.ID ON dbo.t_Loan.ID = CF_E.LoanID LEFT OUTER JOIN
                      dbo.v_Note_Last AS N_L ON CF_E.LoanID = N_L.LoanID LEFT OUTER JOIN
                      usix.t_Dealer ON CF_E.CompanyID_Raw = usix.t_Dealer.CompanyID AND CF_E.[Dealer Code] = usix.t_Dealer.[Dealer Code]
WHERE     (ISNULL(dbo.t_Loan.ContractHolderID, 0) IN
                          (SELECT     CHID
                            FROM          dbo.fs_ContractHolders() AS fs_ContractHolders_1))

GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane1', @value = N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1[50] 4[25] 3) )"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1[56] 3) )"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[41] 4[34] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1[53] 4) )"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 8
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "N_L"
            Begin Extent = 
               Top = 210
               Left = 38
               Bottom = 318
               Right = 204
            End
            DisplayFlags = 280
            TopColumn = 7
         End
         Begin Table = "ts_State"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 99
               Right = 189
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "ts_TimeZone"
            Begin Extent = 
               Top = 15
               Left = 254
               Bottom = 108
               Right = 405
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "CF_E"
            Begin Extent = 
               Top = 6
               Left = 416
               Bottom = 114
               Right = 615
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_Car"
            Begin Extent = 
               Top = 6
               Left = 653
               Bottom = 114
               Right = 856
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "N_PTP_O"
            Begin Extent = 
               Top = 6
               Left = 894
               Bottom = 114
               Right = 1045
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_BuyBack"
            Begin Extent = 
               Top = 102
               Left = 38
               Bottom = 210
               Right = 193
            End
            DisplayFlags = 280', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_CF_Test';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane2', @value = N'TopColumn = 0
         End
         Begin Table = "S_BB"
            Begin Extent = 
               Top = 102
               Left = 231
               Bottom = 210
               Right = 382
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_ContractHolder"
            Begin Extent = 
               Top = 114
               Left = 420
               Bottom = 222
               Right = 576
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_Loan"
            Begin Extent = 
               Top = 114
               Left = 614
               Bottom = 222
               Right = 794
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "S"
            Begin Extent = 
               Top = 114
               Left = 832
               Bottom = 222
               Right = 983
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_Dealer (usix)"
            Begin Extent = 
               Top = 210
               Left = 242
               Bottom = 318
               Right = 393
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      PaneHidden = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 12
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 12795
         Alias = 1605
         Table = 1965
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_CF_Test';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPaneCount', @value = 2, @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_CF_Test';

